<?php
namespace Back\Model;
use Think\Model;

class GoodsModel extends Model{
    protected $_auto = array(
        array("add_time","time",1,"function"),
        array("upd_time","time",3,"function"),
    );

    //插入数据之前的方法
    protected function _before_insert(&$data,$options){
        //上传商品大图
         $config = array(
            'mimes'         =>  array(), //允许上传的文件MiMe类型
            'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
            'exts'          =>  array(), //允许上传的文件后缀
            'autoSub'       =>  false, //自动子目录保存文件

            'rootPath'      =>  './Public/goodsimg/', //保存根路径
        );

        $upload = new \Think\Upload($config);
        $uploadinfo = $upload -> uploadOne($_FILES["goods_big_logo"]);
        $data["goods_big_logo"] = $config["rootPath"].$uploadinfo["savename"];

        //制作商品大图缩略图
        $img = new \Think\Image();
        $img -> open($data["goods_big_logo"]);
        $img -> thumb(60,60);
        $small_img = $config["rootPath"]."small_".$uploadinfo["savename"];
        $img -> save($small_img);
        $data["goods_small_logo"] = $small_img;
    }

    protected function _after_insert($data,$options) {
        $goods_id = $data["goods_id"];
        //商品相册上传
        $config = array(
            'mimes'         =>  array(), //允许上传的文件MiMe类型
            'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
            'exts'          =>  array(), //允许上传的文件后缀
            'autoSub'       =>  false, //自动子目录保存文件
            'rootPath'      =>  './Public/goodsimg/', //保存根路径
        );

        $upload = new \Think\Upload($config);
        $uploadinfo = $upload -> upload(array($_FILES['img_url']));
        $img = new \Think\Image();

        foreach ($uploadinfo as $vv) {
            $bigpath = $config["rootPath"] . $vv["savename"];
            //制作商品相册缩略图
            $img -> open($bigpath);
            $img -> thumb(100,100);
            $smallpath = $config["rootPath"]."smallpics_".$vv["savename"];
            $img -> save($smallpath);

            $picsinfo[] = array(
                "goods_id" => $goods_id,
                "pics_big" => $bigpath,
                "pics_small" => $smallpath
            );
        }
        //相册图片添加
        D("Pics")-> addAll($picsinfo);
    }

    //获取商品列表数据和分页类的实现
    public function fetchData(){
        if(empty($_GET['p'])){
            $_GET['p'] = 0;
        }
        $list = $this -> where("is_del = '不删除'") -> order("goods_id desc") -> page($_GET['p'].',5') ->select();
        $count = $this ->count();
        $page = new \Think\Page($count,5);
        // 分页显示定制
        $page ->lastSuffix =false;
        $page ->setConfig("prev","上一页");
        $page ->setConfig("next","下一页");
        $page->setConfig('first','首页');
        $page->setConfig('last','末页');
        $show = $page -> show();
        return array(
            "show"=>$show,
            "list"=>$list,
        );
    }

    // 更新数据前的回调方法
    protected function _before_update(&$data,$options) {

        //判断有新的商品大图上传，若上传删除原来的图片

        if($_FILES["goods_big_logo_upd"]["error"] === 0){
            $goods_id = I("get.goods_id");
            $info = $this -> field("goods_big_logo,goods_small_logo") -> find($goods_id);
            unlink($info["goods_big_logo"]);
            unlink($info["goods_small_logo"]);


        //上传商品大图
        $config = array(
            'mimes'         =>  array(), //允许上传的文件MiMe类型
            'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
            'exts'          =>  array(), //允许上传的文件后缀
            'autoSub'       =>  false, //自动子目录保存文件

            'rootPath'      =>  './Public/goodsimg/', //保存根路径
        );

        $upload = new \Think\Upload($config);
        $uploadinfo = $upload -> uploadOne($_FILES["goods_big_logo_upd"]);
        $data["goods_big_logo"] = $config["rootPath"].$uploadinfo["savename"];

        //制作商品大图缩略图
        $img = new \Think\Image();
        $img -> open($data["goods_big_logo"]);
        $img -> thumb(60,60);
        $small_img = $config["rootPath"]."small_".$uploadinfo["savename"];
        $img -> save($small_img);
        $data["goods_small_logo"] = $small_img;
        }


        if($_FILES["img_url_upd"]["error"][0] === 0){
            $goods_id = I("get.goods_id");
            //商品相册上传
            $config = array(
                'mimes'         =>  array(), //允许上传的文件MiMe类型
                'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
                'exts'          =>  array(), //允许上传的文件后缀
                'autoSub'       =>  false, //自动子目录保存文件
                'rootPath'      =>  './Public/goodsimg/', //保存根路径
            );

            $upload = new \Think\Upload($config);
            $uploadinfo = $upload -> upload(array($_FILES['img_url_upd']));
            $img = new \Think\Image();

            foreach ($uploadinfo as $vv) {
                $bigpath = $config["rootPath"] . $vv["savename"];
                //制作商品相册缩略图
                $img -> open($bigpath);
                $img -> thumb(100,100);
                $smallpath = $config["rootPath"]."smallpics_".$vv["savename"];
                $img -> save($smallpath);

                $picsinfo[] = array(
                    "goods_id" => $goods_id,
                    "pics_big" => $bigpath,
                    "pics_small" => $smallpath
                );
            }
            //相册图片添加
            D("Pics")-> addAll($picsinfo);
        }
    }
}